package com.goods.business.service.impl;

import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductStockService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductStockServiceImpl implements ProductStockService {

    @Autowired
    private ProductStockMapper productStockMapper;
    @Autowired
    private ProductMapper productMapper;

    //废弃
    @Override
    public PageVO<ProductStockVO> findAllStocks(Integer pageNum, Integer pageSize) {

        List<ProductStock> productStocks = productStockMapper.selectAll();

        //偷天换日 用查出的product对象 转为 productStockVo对象
        List<ProductStockVO> stockVos = productStocks.stream().map(productStock -> {
            ProductStockVO productStockVO = new ProductStockVO();

            String pNum = productStock.getPNum();

            Example o = new Example(Product.class);
            Example.Criteria criteria1 = o.createCriteria();
            criteria1.andEqualTo("pNum", pNum);
            Product product = productMapper.selectOneByExample(o);

            BeanUtils.copyProperties(productStock, productStockVO);
            productStockVO.setName(product.getName());
            productStockVO.setModel(product.getModel());
            productStockVO.setUnit(product.getUnit());
            productStockVO.setRemark(product.getRemark());
            productStockVO.setImageUrl(product.getImageUrl());
            return productStockVO;
        }).collect(Collectors.toList());

        PageInfo<ProductStockVO> info = new PageInfo<>(stockVos);

        return new PageVO<>(info.getTotal(), stockVos);

    }

    //按条件 分页查询物资库存
    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize,
                                                    String categorys,String name,
                                                    ProductStockVO productStockVO) {
        Example o = new Example(Product.class);

        Example.Criteria criteria = o.createCriteria();

        List<ProductStock> productStocks = productStockMapper.selectByExample(o);

        List<ProductStockVO> stockVos = productStocks.stream().map(productStock -> {

            String pNum = productStock.getPNum();
            Example o1 = new Example(Product.class);
            Example.Criteria criteria1 = o1.createCriteria();
            criteria1.andEqualTo("pNum", pNum);
            criteria1.andEqualTo("status", 0);
            if (null != name && !"".equals(name)) {
                criteria1.andLike("name", "%" + name + "%");
            }

            if (!StringUtils.isEmpty(categorys)) {
                String[] categoryKeys = categorys.split(",");
                if (categoryKeys.length == 1) {
                    criteria1.andEqualTo("oneCategoryId", categoryKeys[0]);
                }
                if (categoryKeys.length == 2) {
                    criteria1.andEqualTo("twoCategoryId", categoryKeys[1]);
                }
                if (categoryKeys.length == 3) {
                    criteria1.andEqualTo("threeCategoryId", categoryKeys[2]);
                }
            }
            Product product = productMapper.selectOneByExample(o1);
            ProductStockVO productStockVO1 = new ProductStockVO();
            if (null != product) {
                BeanUtils.copyProperties(productStock, productStockVO1);
                productStockVO1.setName(product.getName());
                productStockVO1.setModel(product.getModel());
                productStockVO1.setUnit(product.getUnit());
                productStockVO1.setRemark(product.getRemark());
                productStockVO1.setImageUrl(product.getImageUrl());
            }
            if (null != productStockVO1){
                return productStockVO1;
            }else {
                return null;
            }
            //TODO 物资名称name条件查询无效&前端会有空值显示
        }).collect(Collectors.toList());

        PageInfo<ProductStockVO> info = new PageInfo<>(stockVos);

        return new PageVO<>(info.getTotal(), stockVos);
    }


}
